//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
    item : {
       display     : 'hide',
       error_info  : '操作太过频繁',
    },
    navigator: {
      nav_one: ['/image/nav3.png','nav1'],
      nav_two: ['/image/nav5.png',''],
      nav_three: ['/image/nav2.png',''],
      nav_type: '/pages/snookerbattle/snookerbattle'
    
    },
    battle_id      : '',
    player1_name   : '丁 先生',
    player2_name   : '奥 先生',
    player1_name_2 : '',
    player2_name_2 : '',
    player1_score  : '00',
    player2_score  : '00',
    player1_break  : 0,
    player2_break  : '',
    player1_point  : 0,
    player2_point  : 0,
    sum_frame      : 7,
    last_time      : '',
    event_name     : '赛事名称',
    event_stage    : '比赛阶段',
    button_1  : '/image/snooker/bq.png',
    button_2  : '/image/snooker/b1.png',
    button_3  : '/image/snooker/b2.png',
    button_4  : '/image/snooker/b3.png',
    button_5  : '/image/snooker/b4.png',
    button_6  : '/image/snooker/b5.png',
    button_7  : '/image/snooker/b6.png',
    button_8  : '/image/snooker/b7.png',
    button_9  : '/image/snooker/c1.png',
    button_10 : '/image/snooker/c2.png',
    button_11 : '/image/snooker/c3.png',
    button_12 : '/image/snooker/c4.png',
    action_player :1,
    actionSheetHidden: true
  },
  //分享
  onShareAppMessage: function () {
      return {
          title: wx.getStorageSync('wxapp_config').share_title,
          path: '/pages/index/index',
      }
  },

  //获取对阵的信息
  onShow: function () {
    var that = this;
    var battle_info = wx.getStorageSync('battle_info');
    var nav_type = battle_info.battle_type == 2 ? '/pages/snookerbattle/snookerbattle' : '/pages/battle/battle';
    this.setData({ navigator: { nav_type: nav_type } });
    if (battle_info) {
      this.setData({
        battle_id: battle_info.battle_id,
        player1_name: battle_info.player1_name,
        player2_name: battle_info.player2_name,
        event_stage: battle_info.event_stage,
        sum_frame: battle_info.sum_frame,
        last_time: Date.now() - 1000,
        event_name: wx.getStorageSync('event_info').event_name,
      })
      //初始化比分
      this.updateScore();
      //双打判断根据选手姓名中是否有逗号判断
      if (battle_info.player1_name.indexOf(',') > 0) {
        var player1_name_array = battle_info.player1_name.split(",");
        this.setData({
          player1_name: player1_name_array[0],
          player1_name_2: player1_name_array[1]
        })
      } else if (battle_info.player1_name.indexOf('，') > 0) {
        var player1_name_array = battle_info.player1_name.split("，");
        this.setData({
          player1_name: player1_name_array[0],
          player1_name_2: player1_name_array[1]
        })
      }

      if (battle_info.player2_name.indexOf(',') > 0) {
        var player2_name_array = battle_info.player2_name.split(",");
        this.setData({
          player2_name: player2_name_array[0],
          player2_name_2: player2_name_array[1]
        })
      } else if (battle_info.player2_name.indexOf('，') > 0) {
        var player2_name_array = battle_info.player2_name.split("，");
        this.setData({
          player2_name: player2_name_array[0],
          player2_name_2: player2_name_array[1]
        })
      }
    }
  },

  //按下切换选手按钮
  swithPlayerStart:function(){
    this.setData({ button_1: '/image/snooker/bqq.png'})
  },
  swithPlayerEnd: function () {
    if (wx.vibrateShort) {
       wx.vibrateShort({});
    }
    this.setData({ button_1: '/image/snooker/bq.png' });
    if (this.preventFastOperation()){
      var action_player = this.data.action_player == 1 ? 2:1;
      wx.setStorage({key: 'action_player',data: action_player});
      this.setData({ action_player: action_player });
      this.saveScore(action_player,0);
    }
  },

  //按下加1分的按钮
  addScore1Start:function(){
    this.setData({ button_2: '/image/snooker/b11.png' })
  },
  addScore1End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_2: '/image/snooker/b1.png' });
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player,1);
    }
  },
  //按下加2分的按钮
  addScore2Start: function () {
    this.setData({ button_3: '/image/snooker/b22.png' })
  },
  addScore2End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_3: '/image/snooker/b2.png' })
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player, 2);
    }
  },
  //按下加3分的按钮
  addScore3Start: function () {
    this.setData({ button_4: '/image/snooker/b33.png' })
  },
  addScore3End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_4: '/image/snooker/b3.png' })
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player, 3);
    }
  },
  //按下加4分的按钮
  addScore4Start: function () {
    this.setData({ button_5: '/image/snooker/b44.png' })
  },
  addScore4End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_5: '/image/snooker/b4.png' })
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player, 4);
    }
  },
  //按下加5分的按钮
  addScore5Start: function () {
    this.setData({ button_6: '/image/snooker/b55.png' })
  },
  addScore5End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_6: '/image/snooker/b5.png' })
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player, 5);
    }
  },
  //按下加6分的按钮
  addScore6Start: function () {
    this.setData({ button_7: '/image/snooker/b66.png' })
  },
  addScore6End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_7: '/image/snooker/b6.png' })
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player, 6);
    }
  },
  //按下加7分的按钮
  addScore7Start: function () {
    this.setData({ button_8: '/image/snooker/b77.png' })
  },
  addScore7End: function () {
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_8: '/image/snooker/b7.png' })
    if (this.preventFastOperation()) {
      this.saveScore(this.data.action_player, 7);
    }
  },
  //按下获取按钮
  winStart:function(){
    this.setData({ button_9: '/image/snooker/c11.png' })
  },
  winEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_9: '/image/snooker/c1.png' });
    var action_player = this.data.action_player;
    var winner = action_player == 1? '红角':'蓝角';
    var player1_score = this.data.player1_score;  
    var player2_score = this.data.player2_score;

    var that = this;
    //验证比分是否为最大值
    if(this.checkScore(Number(player1_score),Number(player2_score))){
      wx.showModal({
        title: '提示',
        content: '确定判定'+winner+'获胜？',
        success: function (res) {
          if (res.confirm) {
            //确定获胜
            if(action_player == 1){
                player1_score = Number(player1_score)+1;
                player1_score = player1_score < 10 ? '0'+player1_score:player1_score;
                var player1_break = '';
                var player2_break = 0;
                action_player = 2;
            }else if(action_player == 2){
              player2_score = Number(player2_score) + 1;
              player2_score = player2_score < 10 ? '0' + player2_score : player2_score;
              var player1_break = 0;
              var player2_break = '';
              action_player = 1;
            }
            var snooker_battle_info = wx.getStorageSync('snooker_battle_info') || [[1, '00', 0, 0, '00', '', 0]];

            snooker_battle_info.push([action_player, player1_score, player1_break,0, player2_score, player2_break,0]);
            
            wx.setStorage({
              key: 'snooker_battle_info',
              data: snooker_battle_info,
              success: function (res) {
                that.updateScore();
              }
            })
          }
        }
      })
    }
  },

  //按下撤销按钮
  revokeStart:function(){
    this.setData({ button_10: '/image/snooker/c22.png' })
  },
  revokeEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_10: '/image/snooker/c2.png' })
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要撤销上一步操作吗？',
      success: function (res) {
        if (res.confirm) {
          var snooker_battle_info = wx.getStorageSync('snooker_battle_info');
          if (snooker_battle_info.length >= 2) {
            snooker_battle_info.pop();
            wx.setStorage({
              key: 'snooker_battle_info',
              data: snooker_battle_info,
              success: function (res) {
                that.updateScore();
              }
            })
          }
        }
      }
    })
  },
  //按下重开按钮
  reopenStart:function(){
    this.setData({ button_11: '/image/snooker/c33.png' })
  },
  reopenEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_11: '/image/snooker/c3.png' })
    //重新开启本局比赛
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定重新开启本局比赛吗',
      success: function (res) {
        if (res.confirm) {
          var snooker_battle_info = wx.getStorageSync('snooker_battle_info') || [[1, '00', 0, 0, '00', '', 0]];

          snooker_battle_info.push([1,that.data.player1_score, 0, 0,that.data.player2_score, '', 0]);

          wx.setStorage({
            key: 'snooker_battle_info',
            data: snooker_battle_info,
            success: function (res) {
              that.updateScore();
            }
          })

        }
      }
    })
  },
  //按下犯规按钮
  foulStart:function(){
    this.setData({ button_12: '/image/snooker/c44.png' })
  },
  foulEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
    this.setData({ button_12: '/image/snooker/c4.png' });
    var that = this;
    var snooker_battle_info = wx.getStorageSync('snooker_battle_info') || [[1, '00', 0, 0, '00', '', 0]];
    wx.showActionSheet({
      itemList: ['4','5','6','7'],
      success: function (res) {
        if (res.tapIndex || res.tapIndex === 0){
          var itemList = [4,5,6,7];
          var point = Number(itemList[res.tapIndex]);
          var battle_info = [that.data.action_player,
                             that.data.player1_score,
                             that.data.player1_break,
                             that.data.player1_point,
                             that.data.player2_score,
                             that.data.player2_break,
                             that.data.player2_point
                            ];
          that.data.action_player == 1 ? battle_info[6] = (Number(battle_info[6]) + point) : battle_info[3] = (Number(battle_info[3]) + point);
          console.log(snooker_battle_info);
          snooker_battle_info.push(battle_info);
          wx.setStorage({
            key: 'snooker_battle_info',
            data: snooker_battle_info,
            success: function (res) {
               that.updateScore();
            }
          })
        }
      }
    })
  },
  //更新比分
  updateScore:function(){
     var snooker_battle_info = wx.getStorageSync('snooker_battle_info') || '';
         snooker_battle_info = snooker_battle_info != '' ? (snooker_battle_info[snooker_battle_info.length - 1]) : snooker_battle_info = [1, '00', 0, 0, '00', '', 0];
  
     this.setData({
        player1_score: snooker_battle_info[1],
        player2_score: snooker_battle_info[4],
        action_player: snooker_battle_info[0],
        player1_break: snooker_battle_info[2],
        player2_break: snooker_battle_info[5],
        player1_point: snooker_battle_info[3],
        player2_point: snooker_battle_info[6]
     })
     this.updateServerScore(snooker_battle_info);


  },

  //更新远程比分
  updateServerScore: function (snooker_battle_info){
    //修改远程比分
    var that = this;
    var url  = getApp().data.servser + '/Battle/editSnookerScore';
    wx.request({
      url: url,
      data: {
        'battle_id': that.data.battle_id,
        'player1_score' : Number(that.data.player1_score),
        'player2_score' : Number(that.data.player2_score),
        'snooker_score' : {
          player_action : snooker_battle_info[0],
          player1_score : snooker_battle_info[1],
          player1_break : snooker_battle_info[2],
          player1_point : snooker_battle_info[3],
          player2_score : snooker_battle_info[4],
          player2_break : snooker_battle_info[5],
          player2_point : snooker_battle_info[6],

        }
      },
      header: {
        'content-type': 'application/json'
      },
      method: "GET",
      success: function (res) {

      }
    })
  },

  //跳转到信息编辑的页面
  toEditbattle:function(){
      if(this.remindAddBattleInfo()){
         wx.navigateTo({
            url: '/pages/editbattle/editbattle'
         })
      }
  },

  //修改比分
  saveScore:function(player,score){
      var player_break = 0;
      var player_point = 0;
      var battle_info  = [];
      if (player == 1) {
          player_break = Number(this.data.player1_break) + score;
          player_point = Number(this.data.player1_point) + score;
          battle_info = [
            this.data.action_player,
            this.data.player1_score,
            player_break,
            player_point,
            this.data.player2_score,
            '',
            this.data.player2_point
          ]
      } else {
          player_break = Number(this.data.player2_break) + score;
          player_point = Number(this.data.player2_point) + score;
          battle_info = [
            this.data.action_player,
            this.data.player1_score,
            '',
            this.data.player1_point,
            this.data.player2_score,
            player_break,
            player_point
          ]
      }
      var snooker_battle_info = wx.getStorageSync('snooker_battle_info') || [[1, '00', 0, 0, '00', '', 0]]; 

      snooker_battle_info.push(battle_info);
      var that = this;
      wx.setStorage({
        key: 'snooker_battle_info',
        data: snooker_battle_info,
        success: function (res) {
           that.updateScore();
        } 
      })
  },

  pushStorageSync:function(){
    var snooker_battle_info = wx.getStorageSync('snooker_battle_info') || [[1,'00',0,0,'00','',0]];
    snooker_battle_info.push([
      this.data.action_player,
      this.data.player1_score,
      this.data.player1_break,
      this.data.player1_point,
      this.data.player2_score,
      this.data.player2_break,
      this.data.player2_point
    ])
    wx.setStorage({
      key: 'snooker_battle_info',
      data: snooker_battle_info
    })
},

  //防止用户操作过快
  preventFastOperation:function(){
      var that =this;
      var now_time = Date.now();
      if(this.remindAddBattleInfo()){
          if(now_time-this.data.last_time>500){
              this.data.last_time = now_time;
              return true;
          }else{
              that.setData({
                  item: {
                      display    :'show',
                      error_info :'操作太过频繁'
                  }
              })
              this.closeErrorInfo();
              return false;
          }          
      }else{
          return false;
      } 
   },

    //定时器关闭提示信息
  closeErrorInfo:function(){
      var that = this;
      setTimeout(function(){
          that.setData({ item: {display:'hide'} })
      },'500')
  },
    //如果对局信息为空提示用户添加对局信息
  remindAddBattleInfo:function(){
      var that = this;
      var battle_info = wx.getStorageSync('battle_info');
      if(!battle_info){
          that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加对局信息'
              }
          })
          this.closeErrorInfo();
          return false; 
      }else{
            return true;
      }
  },
  //验证比分是否达到最大值
  //验证比分是否达到最大值
  checkScore: function (player1_score,player2_score) {
    //获取比赛的总局数
    var that = this;
    var sum_frame = Number(this.data.sum_frame);
    var big_score = Math.ceil((sum_frame + 1) / 2);

    if (player1_score>=big_score || player2_score>=big_score) {
      that.setData({
        item: {
          display: 'show',
          error_info: '比分已达到最大值'
        }
      })
      this.closeErrorInfo();
      return false;
    } else {
       return true;
    }
  },

    //防止用户操作过快
  preventFastOperation: function () {
    var that = this;
    var now_time = Date.now();
    if (now_time - this.data.last_time > 500) {
      this.data.last_time = now_time;
      return true;
    } else {
      that.setData({
        item: {
          display: 'show',
          error_info: '操作太过频繁'
        }
      })
      this.closeErrorInfo();
      return false;
    }
  },
})
